﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRM.Entity;
using DP.Common;
using System.Web.UI;
using System.Web;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DP.Web.UI;

namespace CRM.BLL
{
    [Serializable] 
    public class UserLoginInfo
    {
        private const string LoginSession = "LoginSession";
        
        private DateTime _loginTime;
        private ClientInfo _clientInfo;

        public ClientInfo ClientInfo
        {
            get { return _clientInfo; }
            set { _clientInfo = value; }
        }

        private T_USER_Entity _userEntity;
        private T_BUSINESSDIVISION_Entity _businessdivisionEntity;
        private T_DEPARTMENT_Entity _departmentEntity;
        private T_MARKETDEPARTMENT_Entity _marketDepartmentEntity;
        private T_DEALER_Entity _dealerEntity;
        private T_SUPPLIER_Entity _supplierEntity;       
        private T_SERVICEPROVIDER_Entity _serviceProviderEntity;        
        private T_ROLE_Entity _roleEntity;
        private List<V_ROLEMENU_Entity> _roleMenuList;
        private List<V_ROLEPERMISSION_Entity> _rolePermissionList;



        public DateTime LoginTime
        {
            get { return _loginTime; }
            set { _loginTime = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public T_USER_Entity UserEntity
        {
            get { return _userEntity; }
            set { _userEntity = value; }
        }

        /// <summary>
        /// Gets or sets the businessdivision entity.
        /// </summary>
        /// <value>
        /// The businessdivision entity.
        /// </value>
        public T_BUSINESSDIVISION_Entity BusinessdivisionEntity
        {
            get { return _businessdivisionEntity; }
            set { _businessdivisionEntity = value; }
        }

        #region 部门
        /// <summary>
        /// Gets or sets the department entity.
        /// </summary>
        /// <value>
        /// The department entity.
        /// </value>
        public T_DEPARTMENT_Entity DepartmentEntity
        {
            get { return _departmentEntity; }
            set { _departmentEntity = value; }
        }
        /// <summary>
        /// Gets or sets the market department entity.
        /// </summary>
        /// <value>
        /// The market department entity.
        /// </value>
        public T_MARKETDEPARTMENT_Entity MarketDepartmentEntity
        {
            get { return _marketDepartmentEntity; }
            set { _marketDepartmentEntity = value; }
        }
        /// <summary>
        /// Gets or sets the dealer entity.
        /// </summary>
        /// <value>
        /// The dealer entity.
        /// </value>
        public T_DEALER_Entity DealerEntity
        {
            get { return _dealerEntity; }
            set { _dealerEntity = value; }
        }
        /// <summary>
        /// Gets or sets the supplier entity.
        /// </summary>
        /// <value>
        /// The supplier entity.
        /// </value>
        public T_SUPPLIER_Entity SupplierEntity
        {
            get { return _supplierEntity; }
            set { _supplierEntity = value; }
        }
        /// <summary>
        /// Gets or sets the service provider entity.
        /// </summary>
        /// <value>
        /// The service provider entity.
        /// </value>
        public T_SERVICEPROVIDER_Entity ServiceProviderEntity
        {
            get { return _serviceProviderEntity; }
            set { _serviceProviderEntity = value; }
        } 
        #endregion

        /// <summary>
        /// Gets or sets the role entity.
        /// </summary>
        /// <value>
        /// The role entity.
        /// </value>
        public T_ROLE_Entity RoleEntity
        {
            get { return _roleEntity; }
            set { _roleEntity = value; }
        }
        /// <summary>
        /// Gets or sets the role menu list.
        /// </summary>
        /// <value>
        /// The role menu list.
        /// </value>
        public List<V_ROLEMENU_Entity> RoleMenuList
        {
            get { return _roleMenuList; }
            set { _roleMenuList = value; }
        }
        /// <summary>
        /// Gets or sets the role permission list.
        /// </summary>
        /// <value>
        /// The role permission list.
        /// </value>
        public List<V_ROLEPERMISSION_Entity> RolePermissionList
        {
            get { return _rolePermissionList; }
            set { _rolePermissionList = value; }
        }

        /// <summary>
        /// Writes the log.
        /// </summary>
        /// <param name="logType">Type of the log.</param>
        /// <param name="operationContent">Content of the operation.</param>
        /// <param name="operationTitle">The operation title.</param>
        /// <param name="remark">The remark.</param>
        public static void WriteLog(LogType logType, string operationContent, string operationTitle, string remark)
        {
            T_LOG_BLL logBll = new T_LOG_BLL();
            T_LOG_Entity logEntity = new T_LOG_Entity();
            UserLoginInfo _userLoginInfo = UserLoginInfo.GetLoginInfo();
            if (_userLoginInfo == null)
            {
                return;
            }
            logEntity.CREATEPEOPLE = _userLoginInfo._userEntity.USERNAME;
            logEntity.CREATEPEOPLEID = _userLoginInfo._userEntity.USERID;
            logEntity.CREATETIME = DateTime.Now;
            logEntity.DEPARTMENTID = _userLoginInfo._userEntity.DEPARTMENTID;
            logEntity.DEPARTMENTNAME = _userLoginInfo._userEntity.DEPARTMENTNAME;
            logEntity.LOGINLENGTH = (DateTime.Now - _userLoginInfo.LoginTime).TotalSeconds;
            logEntity.LOGINNAME = _userLoginInfo._userEntity.LOGINNAME;
            logEntity.LOGINTIME = _userLoginInfo.LoginTime;
            logEntity.LOGTYPEID = (int)logType;
            logEntity.OPERATIONCONTENT = operationContent;
            logEntity.OPERATIONTITLE = operationTitle;
            logEntity.OPERATIONTYPE = logType.ToString();
            logEntity.PASSWORD = _userLoginInfo._userEntity.PASSWORD;
            logEntity.REMARK = remark;
            logEntity.USERIP = _userLoginInfo._clientInfo.IP;
            //logEntity.USERMACADDRESS = _userLoginInfo._clientInfo.MacAddress;
            logEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            logEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
            logEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
            try
            {
                logBll.Insert(logEntity);
            }
            catch (Exception ex)
            {

            }

        }



        /// <summary>
        /// 设置登录用户信息
        /// </summary>
        /// <param name="entity">The entity.</param>
        public static void SetLoginInfo(UserLoginInfo entity)
        {
            CacheHelper.SetSession(LoginSession, entity);
        }
        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        /// <returns></returns>
        public static UserLoginInfo GetLoginInfo()
        {
            UserLoginInfo loginInfo = CacheHelper.GetSession(LoginSession) as UserLoginInfo;

//#if DEBUG
//            #region 测试用代码正式上线前需删除。

            //if (loginInfo == null)
            //{
            //    loginInfo = new UserLoginInfo();
            //    loginInfo.UserEntity = new T_USER_Entity();
            //    loginInfo.UserEntity.USERID = 0;
            //    loginInfo.UserEntity.USERNAME = "Administrator";
            //    loginInfo.UserEntity.ISSYSTEMADMIN = 1;
            //    loginInfo.UserEntity.BUSINESSDIVISIONCODE = "6";
            //    loginInfo.UserEntity.BUSINESSDIVISIONID = 4;
            //    loginInfo.UserEntity.BUSINESSDIVISIONNAME = "农业装备事业部";
            //    loginInfo.DepartmentEntity = new T_DEPARTMENT_Entity();
            //    loginInfo.DepartmentEntity.DEPARTMENTID = 0;
            //    loginInfo.DepartmentEntity.DEPARTMENTNAME = "系统管理";
            //    loginInfo.RoleEntity = new T_ROLE_Entity();
            //    loginInfo.RoleEntity.ROLEID = 0;
            //    loginInfo.RoleEntity.ROLENAME = "系统管理";
            //    loginInfo.BusinessdivisionEntity = new T_BUSINESSDIVISION_Entity();
            //    loginInfo.BusinessdivisionEntity.BUSINESSDIVISIONCODE = "6";
            //    loginInfo.BusinessdivisionEntity.BUSINESSDIVISIONID = 4;
            //    loginInfo.BusinessdivisionEntity.BUSINESSDIVISIONNAME = "农业装备事业部";
            //    loginInfo.BusinessdivisionEntity.PRESSKEY = "1";
            //    loginInfo.BusinessdivisionEntity.VIRTUALGROUP = "411";

            //}

//            #endregion
//#endif

            return loginInfo;
        }

        /// <summary>
        /// 清除登录用户信息
        /// </summary>
        public static void ClearLoginInfo()
        {
            CacheHelper.ClearSession(LoginSession);
        }
        /// <summary>
        /// 检测用户是否登录
        /// </summary>
        public static void CheckLoginInfo()
        {            
            Page page = HttpContext.Current.Handler as Page;
            if (page != null)
            {
                UserLoginInfo entity = GetLoginInfo();
                if (entity == null)
                {
                    page.Response.Redirect("~/Logout.aspx");
                }
            }
        }
        /// <summary>
        /// 检测用户菜单权限
        /// </summary>
        public static void CheckMenuRight()
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page != null)
            {
                if (IsAdministrator())
                {
                    return;
                }
                foreach (string s in StaticConstant.NotCheckMenuPage)
                {
                    if (s.ToLower() == page.ToString().ToLower())
                    {
                        return;
                    }
                }
                UserLoginInfo entity = GetLoginInfo();
                if (entity == null)
                {
                    page.Response.Redirect("~/RightError.aspx");
                    return;
                }
                if (entity.RoleMenuList == null)
                {
                    page.Response.Redirect("~/RightError.aspx");
                    return;
                }
                V_ROLEMENU_Entity rmEntity = entity.RoleMenuList.Find(delegate(V_ROLEMENU_Entity en)
                                                                          {
                                                                              string path = (en.PAGEPATH.TrimEnd('/') + "/" + en.PAGENAME.Trim()).ToLower();
                                                                              int index = path.IndexOf('?');
                                                                              if(index>=0)
                                                                              {
                                                                                  path = path.Substring(0, index);
                                                                              }
                                                                              if (page.Request.Url.PathAndQuery.ToLower().IndexOf(path) > 0)
                                                                                  return true;
                                                                              else return false;
                                                                          });
                
                if (rmEntity == null)
                {
                    page.Response.Redirect("~/RightError.aspx");
                    return;
                }
                page.Header.Title = rmEntity.MENUNAME;
            }
        }
        /// <summary>
        /// 检测用户菜单权限
        /// Checks the menu right.
        /// </summary>
        /// <param name="menu">The menu.</param>
        /// <returns></returns>
        public static bool CheckMenuRight(string menu)
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page == null)
            {
                return false;
            }
            if (IsAdministrator())
            {
                return true;
            }
            UserLoginInfo entity = GetLoginInfo();
            if (entity == null)
            {
                return false;
            }
            if (entity.RoleMenuList == null)
            {
                return false;
            }
            V_ROLEMENU_Entity rmEntity = entity.RoleMenuList.Find(delegate(V_ROLEMENU_Entity en) { if (menu.ToLower().Equals((en.PAGEPATH.TrimEnd('/') + "/" + en.PAGENAME.Trim()).ToLower())) return true; else return false; });

            if (rmEntity == null)
            {
                return false;
            }
            return true;

        }
        /// <summary>
        /// 设置界面操作按钮权限
        /// </summary>
        public static void CheckPermissionRight(string PageClassName)
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page == null)
            {
                return;
            }
            if (IsAdministrator())
            {
                return;
            }
            UserLoginInfo entity = GetLoginInfo();
            List<V_ROLEPERMISSION_Entity> sublist;
            if (entity == null)
            {
                sublist = new List<V_ROLEPERMISSION_Entity>();
            }
            else if (entity.RolePermissionList == null)
            {
                sublist = new List<V_ROLEPERMISSION_Entity>();
            }
            else
            {
                sublist = entity.RolePermissionList.FindAll(delegate(V_ROLEPERMISSION_Entity en) { if (en.PAGECLASSNAME.ToLower() == PageClassName.ToString().ToLower()) return true; else return false; });
            }
            SetButtonControlVisible(page, sublist, new List<string> { "Search", "btnCancel", "btnClose", "btnUpload" });
        }

        /// <summary>
        /// 
        /// Checks the permission right.
        /// </summary>
        /// <param name="CommandName">Name of the command.</param>
        /// <returns></returns>
        public static bool IsHasPermissionRight(string CommandName)
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page == null)
            {
                return false;
            }
            if (IsAdministrator())
            {
                return true;
            }
            UserLoginInfo entity = GetLoginInfo();
            List<V_ROLEPERMISSION_Entity> sublist = entity.RolePermissionList.FindAll(delegate(V_ROLEPERMISSION_Entity en) { if (en.PAGECLASSNAME.ToLower() == page.ToString().ToLower()) return true; else return false; });
            return sublist.Exists(p => p.COMANDNAME.ToLower() == CommandName.ToLower());
            //if (entity == null)
            //{
            //    sublist = new List<V_ROLEPERMISSION_Entity>();
            //}
            //else if (entity.RolePermissionList == null)
            //{
            //    sublist = new List<V_ROLEPERMISSION_Entity>();
            //}
            //else
            //{
            //    sublist = entity.RolePermissionList.FindAll(delegate(V_ROLEPERMISSION_Entity en) { if (en.PAGECLASSNAME.ToLower() == page.ToString().ToLower()) return true; else return false; });
            //}
            //SetButtonControlVisible(page, sublist, new List<string> { "Search", "btnCancel", "btnClose", "btnUpload" });
        }

        /// <summary>
        /// Checks the permission right.
        /// </summary>
        /// <param name="PageClassName">Name of the page class.</param>
        /// <param name="CommandName">Name of the command.</param>
        /// <returns></returns>
        public static bool IsHasPermissionRight(string PageClassName, string CommandName)
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page == null)
            {
                return false;
            }
            if (IsAdministrator())
            {
                return true;
            }
            UserLoginInfo entity = GetLoginInfo();
            List<V_ROLEPERMISSION_Entity> sublist = entity.RolePermissionList.FindAll(delegate(V_ROLEPERMISSION_Entity en) { if (en.PAGECLASSNAME.ToLower() == PageClassName.ToString().ToLower()) return true; else return false; });
            return sublist.Exists(p => p.COMANDNAME.ToLower() == CommandName.ToLower());
            //if (entity == null)
            //{
            //    sublist = new List<V_ROLEPERMISSION_Entity>();
            //}
            //else if (entity.RolePermissionList == null)
            //{
            //    sublist = new List<V_ROLEPERMISSION_Entity>();
            //}
            //else
            //{
            //    sublist = entity.RolePermissionList.FindAll(delegate(V_ROLEPERMISSION_Entity en) { if (en.PAGECLASSNAME.ToLower() == page.ToString().ToLower()) return true; else return false; });
            //}
            //SetButtonControlVisible(page, sublist, new List<string> { "Search", "btnCancel", "btnClose", "btnUpload" });
        }

        /// <summary>
        /// Generators the button confirm info.
        /// </summary>
        /// <returns></returns>
        public static string GeneratorButtonConfirmInfo()
        {
            Random random = new Random();
            string strButtonConfirmInfo = random.Next(10000, 99999).ToString();
            CacheHelper.SetSession("ButtonConfirmInfo", strButtonConfirmInfo);
            return strButtonConfirmInfo;
        }

        /// <summary>
        /// Gets the button confirm info.
        /// </summary>
        /// <returns></returns>
        public static string GetButtonConfirmInfo()
        {
            return CacheHelper.GetSession("ButtonConfirmInfo").ToString();
        }

        /// <summary>
        /// Sets the button confirm info.
        /// </summary>
        /// <param name="_buttonConfirmInfo">The _button confirm info.</param>
        public static void SetButtonConfirmInfo(string _buttonConfirmInfo)
        {
            CacheHelper.SetSession("ButtonConfirmInfo", _buttonConfirmInfo);
        }
 

        /// <summary>
        /// 获取当前操作员的部门名称
        /// Gets the name of the department.
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentDepartmentName()
        {
            UserLoginInfo loginInfo = GetLoginInfo();
            return GetDepartmentName(loginInfo);
        }

        /// <summary>
        /// 获取当前操作员的部门名称
        /// Gets the name of the department.
        /// </summary>
        /// <returns></returns>
        public static string GetDepartmentName(UserLoginInfo _userLoginInfo)
        {
            string departmentName = string.Empty;
            try
            {
                switch (GetUserType(_userLoginInfo))
                {
                    case UserType.服务商:
                        {
                            departmentName = _userLoginInfo.ServiceProviderEntity.SERVICEPROVIDERNAME;
                        }
                        break;
                    case UserType.供应商:
                        {
                            departmentName = _userLoginInfo.SupplierEntity.SUPPLIERNAME;
                        }
                        break;
                    case UserType.经销商:
                        {
                            departmentName = _userLoginInfo.DealerEntity.DEALERNAME;
                        }
                        break;
                    case UserType.市场部:
                        {
                            departmentName = _userLoginInfo.MarketDepartmentEntity.MARKETDEPARTMENTNAME;
                        }
                        break;
                    case UserType.中心部门:
                        {
                            departmentName = _userLoginInfo.DepartmentEntity.DEPARTMENTNAME;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {

            }
            return departmentName;
        }


        /// <summary>
        /// 获取当前操作员的部门名称
        /// Gets the name of the department.
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentDepartmentCode()
        {
            UserLoginInfo loginInfo = GetLoginInfo();
            return GetDepartmentCode(loginInfo);           
        }

        /// <summary>
        /// 获取当前操作员的部门名称
        /// Gets the name of the department.
        /// </summary>
        /// <returns></returns>
        public static string GetDepartmentCode(UserLoginInfo _userLoginInfo)
        {
            string departmentCode = string.Empty;
            switch (GetUserType(_userLoginInfo))
            {
                case UserType.服务商:
                    {
                        departmentCode = _userLoginInfo.ServiceProviderEntity.SERVICEPROVIDERCODE;
                    }
                    break;
                case UserType.供应商:
                    {
                        departmentCode = _userLoginInfo.SupplierEntity.SUPPLIERCODE;
                    }
                    break;
                case UserType.经销商:
                    {
                        departmentCode = _userLoginInfo.DealerEntity.DEALERCODE;
                    }
                    break;
                case UserType.市场部:
                    {
                        departmentCode = _userLoginInfo.MarketDepartmentEntity.MARKETDEPARTMENTCODE;
                    }
                    break;
                case UserType.中心部门:
                    {
                        departmentCode = _userLoginInfo.DepartmentEntity.DEPARTMENTCODE;
                    }
                    break;
            }
            return departmentCode;
        }

        /// <summary>
        /// 获取当前操作员的  类型
        /// Gets the type of the current user.
        /// </summary>
        public static UserType GetCurrentUserType()
        {
            UserLoginInfo loginInfo = GetLoginInfo();
            return GetUserType(loginInfo);
        }

        /// <summary>
        /// 获取操作员的  类型
        /// Gets the type of the user.
        /// </summary>
        /// <param name="_userLoginInfo">The _user login info.</param>
        /// <returns></returns>
        public static UserType GetUserType(UserLoginInfo _userLoginInfo)
        {
            UserType userType = UserType.中心部门;
            try
            {
                userType = (UserType)Enum.Parse(typeof(UserType), _userLoginInfo.UserEntity.USERTYPE.Trim());
            }
            catch (Exception ex)
            {

            }
            return userType;
        }

        /// <summary>
        /// Gets the oracle date string.
        /// </summary>
        /// <param name="beginDate">The begin date.</param>
        /// <returns></returns>
        public static string GetOracleBeginDateString(string beginDate)
        {
            return " to_date('" + beginDate + " 00:00:00', 'yyyy-MM-dd hh24:mi.ss') ";
        }

        /// <summary>
        /// Gets the oracle begin date string.
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="beginDate">The begin date.</param>
        /// <returns></returns>
        public static string GetOracleBeginDateString(string fieldName, string beginDate)
        {
            return " " + fieldName + " >= to_date('" + beginDate + " 00:00:00', 'yyyy-MM-dd hh24:mi.ss') ";
        }

        /// <summary>
        /// Gets the oracle end date string.
        /// </summary>
        /// <param name="endDate">The end date.</param>
        /// <returns></returns>
        public static string GetOracleEndDateString(string endDate)
        {
            return " to_date('" + endDate + " 23:59:59', 'yyyy-MM-dd hh24:mi.ss') ";
        }

        /// <summary>
        /// Gets the oracle end date string.
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="endDate">The end date.</param>
        /// <returns></returns>
        public static string GetOracleEndDateString(string fieldName, string endDate)
        {
            return " " + fieldName + " <=  to_date('" + endDate + " 23:59:59', 'yyyy-MM-dd hh24:mi.ss') ";
        }

        /// <summary>
        /// 是否是管理员
        /// </summary>
        /// <returns></returns>
        public static bool IsAdministrator()
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page == null)
            {
                return false;
            }
            UserLoginInfo entity = GetLoginInfo();
            if (entity == null)
            {
                return false;
            }
            if (entity.UserEntity.ISSYSTEMADMIN.ToString() == ((int)IsAdmin.是).ToString())
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据  list 设置 control  子控件  Button 是否显示
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="list">The list.</param>
        /// <param name="strExceptions">The STR exceptions.</param>
        private static void SetButtonControlVisible(Control control, List<V_ROLEPERMISSION_Entity> list, IEnumerable<string> strExceptions)
        {
            V_ROLEPERMISSION_Entity vRolePermissionEntity = null;
            List<string> sList = strExceptions.ToList();
            foreach (Control c in control.Controls)
            {
                if (c is Button)
                {
                    Button btn = c as Button;
                    if (sList.Exists(p => { if (p.ToLower() == btn.CommandName.ToLower()) return true; else return false; }))
                    {
                        btn.Visible = true;
                    }
                    else
                    {
                        if (list == null)
                        {
                            btn.Visible = false;
                        }
                        else
                        {
                            vRolePermissionEntity = list.Find(en =>
                                                                  {
                                                                      if (en.COMANDNAME.Trim().ToLower() == btn.CommandName.ToLower())
                                                                          return true;
                                                                      else
                                                                          return false;
                                                                  });
                            if (vRolePermissionEntity != null)
                            {
                                if (btn.Visible)
                                {
                                    btn.Visible = true;
                                    if (vRolePermissionEntity.ISCONFIRM.ToString() == ((int) IsConfirm.是).ToString())
                                    {
                                        SetButtonConfirm(btn);
                                    }
                                }
                            }
                            else
                            {
                                btn.Visible = false;
                            }
                            //if (list.Exists(en =>
                            //                    {
                            //                        if (en.COMANDNAME.Trim().ToLower() == btn.CommandName.ToLower())
                            //                            return true;
                            //                        else
                            //                            return false;
                            //                    }))
                            //{
                            //    btn.Visible = true;
                            //}
                            //else
                            //{
                            //    btn.Visible = false;
                            //}
                        }
                    }
                }
                else if (c is LinkButton)
                {
                    LinkButton lbtn = c as LinkButton;
                    if (sList.Exists(p => { if (p.ToLower() == lbtn.CommandName.ToLower()) return true; else return false; }))
                    {
                        lbtn.Visible = true;
                    }
                    else
                    {
                        if (list == null)
                        {
                            lbtn.Visible = false;
                        }
                        else
                        {
                            vRolePermissionEntity = list.Find(en =>
                                                                  {
                                                                      if (en.COMANDNAME.Trim().ToLower() == lbtn.CommandName.ToLower())
                                                                          return true;
                                                                      else
                                                                          return false;
                                                                  });
                            if (vRolePermissionEntity != null)
                            {
                                if (lbtn.Visible)
                                {
                                    lbtn.Visible = true;
                                    if (vRolePermissionEntity.ISCONFIRM.ToString() == ((int) IsConfirm.是).ToString())
                                    {
                                        SetButtonConfirm(lbtn);
                                    }
                                }
                            }
                            else
                            {
                                lbtn.Visible = false;
                            }
                                //if (list.Exists(en =>
                                //                    {
                                //                        if (en.COMANDNAME.Trim().ToLower() == lbtn.CommandName.ToLower())
                                //                            return true;
                                //                        else
                                //                            return false;
                                //                    }))
                                //{
                                //    lbtn.Visible = true;
                                //}
                                //else
                                //{
                                //    lbtn.Visible = false;
                                //}
                        }
                    }
                }
                else if (c is ImageButton)
                {
                    ImageButton ibtn = c as ImageButton;
                    if (sList.Exists(p => { if (p.ToLower() == ibtn.CommandName.ToLower()) return true; else return false; }))
                    {
                        ibtn.Visible = true;
                    }
                    else
                    {
                        if (list == null)
                        {
                            ibtn.Visible = false;
                        }
                        else
                        {
                            vRolePermissionEntity = list.Find(en =>
                                                                  {
                                                                      if (en.COMANDNAME.Trim().ToLower() == ibtn.CommandName.ToLower())
                                                                          return true;
                                                                      else
                                                                          return false;
                                                                  });
                            if (vRolePermissionEntity != null)
                            {
                                if (ibtn.Visible)
                                {
                                    ibtn.Visible = true;
                                    if (vRolePermissionEntity.ISCONFIRM.ToString() == ((int) IsConfirm.是).ToString())
                                    {
                                        SetButtonConfirm(ibtn);
                                    }
                                }
                            }
                            else
                            {
                                ibtn.Visible = false;
                            }
                            //if (list.Exists(en =>
                            //                    {
                            //                        if (en.COMANDNAME.Trim().ToLower() == ibtn.CommandName.ToLower())
                            //                            return true;
                            //                        else
                            //                            return false;
                            //                    }))
                            //{
                            //    ibtn.Visible = true;
                            //}
                            //else
                            //{
                            //    ibtn.Visible = false;
                            //}
                        }
                    }
                }
                else if (c is HtmlInputButton)
                {
                    HtmlInputButton hbtn = c as HtmlInputButton;
                    if (sList.Exists(p => { if (p.ToLower() == hbtn.Name.ToLower()) return true; else return false; }))
                    {
                        hbtn.Visible = true;
                    }
                    else
                    {
                        if (list == null)
                        {
                            hbtn.Visible = false;
                        }
                        else
                        {
                            vRolePermissionEntity = list.Find(en =>
                                                                  {
                                                                      if (en.COMANDNAME.Trim().ToLower() == hbtn.Name.ToLower())
                                                                          return true;
                                                                      else
                                                                          return false;
                                                                  });
                            if (vRolePermissionEntity != null)
                            {

                                if (hbtn.Visible)
                                {
                                    hbtn.Visible = true;
                                    if (vRolePermissionEntity.ISCONFIRM.ToString() == ((int) IsConfirm.是).ToString())
                                    {
                                        SetButtonConfirm(hbtn);
                                    }
                                }
                            }
                            else
                            {
                                hbtn.Visible = false;
                            }
                            //if (list.Exists(en =>
                            //                    {
                            //                        if (en.COMANDNAME.Trim().ToLower() == hbtn.Name.ToLower())
                            //                            return true;
                            //                        else
                            //                            return false;
                            //                    }))
                            //{
                            //    hbtn.Visible = true;
                            //}
                            //else
                            //{
                            //    hbtn.Visible = false;
                            //}
                        }
                    }
                }
                else if (c.HasControls())
                {
                    SetButtonControlVisible(c, list, strExceptions);
                }
            }








        }

        /// <summary>
        /// 密码加密
        /// Passwords the encryption.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public static string PasswordEncryption(string password)
        {
            return password;
        }

        /// <summary>
        /// 
        /// Filters the back URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static string FilterBackUrl(string url)
        {
            if(String.IsNullOrEmpty(url))
            {
                return url;
            }
            string strUrl = url.Replace("MainManage/ToDoWorkOrder.aspx", "Main.aspx");
            strUrl = strUrl.Replace("MainManage/ToDoOutbound.aspx", "Main.aspx");
            return strUrl;
        }

        /// <summary>
        /// 过虑电话号码
        /// Filters the tel phone.
        /// </summary>
        /// <param name="tel">The tel.</param>
        /// <returns></returns>
        public static string FilterTelPhone(string tel)
        {
            string _phone = tel;
            if (_phone.IndexOf("013") == 0 || _phone.IndexOf("015") == 0 || _phone.IndexOf("018") == 0 || _phone.IndexOf("014") == 0)
            {
                _phone = _phone.Substring(1);
            }
            return _phone;
        }

        /// <summary>
        /// Filters the back URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="page">The page.</param>
        /// <returns></returns>
        public static string FilterBackUrl(string url, string page)
        {
            if (String.IsNullOrEmpty(url))
            {
                return url;
            }
            string strUrl = url.Replace("MainManage/ToDoWorkOrder.aspx", "Main.aspx");
            strUrl = strUrl.Replace("MainManage/ToDoOutbound.aspx", "Main.aspx");
            strUrl = strUrl.Replace("Left.aspx", page);
            return strUrl;
        }

        /// <summary>
        /// 设置 按钮 二次确认
        /// Sets the button confirm.
        /// </summary>
        /// <param name="btn">The BTN.</param>
        public static void SetButtonConfirm(Button btn)
        {
            btn.OnClientClick = String.Format("if(Page_ClientValidate()) return confirm('您确认要执行 {0} 操作吗？');", btn.Text);
        }

        /// <summary>
        /// Sets the button confirm.
        /// </summary>
        /// <param name="lbtn">The LBTN.</param>
        public static void SetButtonConfirm(LinkButton lbtn)
        {
            lbtn.OnClientClick = String.Format("if(Page_ClientValidate()) return confirm('您确认要执行 {0} 操作吗？');", lbtn.Text);
        }

        /// <summary>
        /// Sets the button confirm.
        /// </summary>
        /// <param name="lbtn">The LBTN.</param>
        public static void SetButtonConfirm(ImageButton ibtn)
        {
            ibtn.OnClientClick = String.Format("if(Page_ClientValidate()) return confirm('您确认要执行 {0} 操作吗？');", ibtn.AlternateText);
        }

        /// <summary>
        /// Sets the button confirm.
        /// </summary>
        /// <param name="lbtn">The LBTN.</param>
        public static void SetButtonConfirm(HtmlInputButton hbtn)
        {
            hbtn.Attributes["click"] = String.Format("if(Page_ClientValidate()) return confirm('您确认要执行 {0} 操作吗？');", hbtn.Value);
        }

        /// <summary>
        /// Writes the work order log.
        /// </summary>
        /// <param name="logFileName">Name of the log file.</param>
        /// <param name="message">The message.</param>
        public static void WriteWorkOrderLog(string logFileName, string message)
        {
            //LogHelper.WriteLog(logFileName, message);
        }


        /// <summary>
        /// Filters the string.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public static string FilterString(string str)
        {
            return str.Trim().Replace("\r", "").Replace("\n", "");
        }

       

    }
}
 